package com.lan.app.api;


import com.lan.app.repository.User;
import com.lan.app.service.IUserService;
import com.lan.app.constant.Constant;
import com.lan.support.intercept.an.Auth;
import com.lan.support.page.Page;
import com.lan.support.page.PageHelper;
import com.lan.support.response.ex.Ex;
import com.lan.support.response.json.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("api/user")
public class UserApi {

    @Resource
    private IUserService userService;

    @Auth
    @ResponseBody
    @RequestMapping(value = "page")
    public R list(User user, PageHelper helper) {
        Page<User> page = userService.page(helper, user);
        return R.OK(page);
    }


    @Auth
    @ResponseBody
    @RequestMapping(value = "modifyPwd")
    public R modifyPwd(@RequestParam Integer id, @RequestParam String pwd) {
        if (StringUtils.isBlank(pwd)) {
            throw Ex.build("密码不能为空");
        }
        userService.modifyPwd(id, pwd);
        return R.OK();
    }

//

    @Auth
    @ResponseBody
    @RequestMapping(value = "updatePwd")
    public R updatePwd(HttpSession session, @RequestParam String oldPwd, @RequestParam String newPwd) {
        if (StringUtils.isBlank(oldPwd) || StringUtils.isBlank(newPwd)) {
            throw Ex.build("密码不能为空");
        }
        User user = (User) session.getAttribute(Constant.User_Info);
        userService.modifyPwd(user.getId(), oldPwd, newPwd);
        session.invalidate();
        return R.OK();
    }


    @Auth
    @ResponseBody
    @RequestMapping(value = "del/{id}")
    public R del(@PathVariable Integer id) {
        userService.del(id);
        return R.OK();
    }


}
